#include <uci.h>
#include "beidou_service.h"
#include "idc_ubus.h"

#define UCI_CONFIG_FILE "/etc/config/idc_beidou"
char *bd_dev = NULL;
int bd_speed = 9600;
/*********************************************
* 载入配置文件,并遍历Section.
*/
void idc_load_config()
{
    struct uci_package * pkg = NULL;
    struct uci_element *e;
    struct uci_context * ctx = NULL;
    char *tmp;
    const char *value;

    ctx = uci_alloc_context(); // 申请一个UCI上下文.
    if (UCI_OK != uci_load(ctx, UCI_CONFIG_FILE, &pkg))
        goto cleanup; //如果打开UCI文件失败,则跳到末尾 清理 UCI 上下文.


    /*遍历UCI的每一个节*/
    uci_foreach_element(&pkg->sections, e)
    {
        struct uci_section *s = uci_to_section(e);

        printf("section s's type is %s.\n",s->type);
        if(!strcmp("conf",s->type)) //this section is a meter
        {
            printf("this seciton is a idc_config.\n");
            if (NULL != (value = uci_lookup_option_string(ctx, s, "device")))
            {
                printf("bd_dev:%s\n", strdup(value));
                bd_dev = strdup(value);
            }
            if (NULL != (value = uci_lookup_option_string(ctx, s, "speed")))
            {
                bd_speed = atoi(strdup(value));
                printf("%s's speed is %d.\n",s->e.name, bd_speed);
            }
        }

    }
    uci_unload(ctx, pkg); // 释放 pkg
    cleanup:
    uci_free_context(ctx);
    ctx = NULL;
}


int main() {
    idc_load_config();
    ep_gather_start();
    ubus_worker_start();
    while (true)
    {
        sleep(1);
    }
    return 0;
}